<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>三方支付 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="配置沙箱环境https:&#x2F;&#x2F;open.alipay.com&#x2F;platform&#x2F;home.htm  进入管理中心  开发服务中的研发服务  进入沙箱环境  设置密钥  用支付宝开放平台开发助手生成公钥复制到  保存设置  设置授权回调地址 1注意：这里需要在后端设置路由协同操作，    流程图 下载SDK插件1pip install python-alipay-sdk --upgrade 发起支付1">
<meta property="og:type" content="article">
<meta property="og:title" content="三方支付">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/01/29/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="配置沙箱环境https:&#x2F;&#x2F;open.alipay.com&#x2F;platform&#x2F;home.htm  进入管理中心  开发服务中的研发服务  进入沙箱环境  设置密钥  用支付宝开放平台开发助手生成公钥复制到  保存设置  设置授权回调地址 1注意：这里需要在后端设置路由协同操作，    流程图 下载SDK插件1pip install python-alipay-sdk --upgrade 发起支付1">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//1.jpg">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//2.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//3.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//4.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//5.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//6.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//7.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//8.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//9.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//10.png">
<meta property="og:image" content="https://mourning-soul.gitee.io/hexo/.io//11.png">
<meta property="article:published_time" content="2021-01-28T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-29T08:08:14.721Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mourning-soul.gitee.io/hexo/.io//1.jpg">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-三方支付" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/01/29/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/" class="article-date">
  <time class="dt-published" datetime="2021-01-28T16:00:00.000Z" itemprop="datePublished">2021-01-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      三方支付
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="配置沙箱环境"><a href="#配置沙箱环境" class="headerlink" title="配置沙箱环境"></a>配置沙箱环境</h2><p><a target="_blank" rel="noopener" href="https://open.alipay.com/platform/home.htm">https://open.alipay.com/platform/home.htm</a></p>
<ul>
<li><p>进入管理中心<br><img src="/hexo/.io//1.jpg" alt="1"></p>
</li>
<li><p>开发服务中的研发服务<br><img src="/hexo/.io//2.png" alt="2"></p>
</li>
<li><p>进入沙箱环境<br><img src="/hexo/.io//3.png" alt="3"></p>
</li>
<li><p>设置密钥<br><img src="/hexo/.io//4.png" alt="4"><br><img src="/hexo/.io//5.png" alt="5"></p>
</li>
<li><p>用支付宝开放平台开发助手生成公钥复制到<br><img src="/hexo/.io//6.png" alt="6"></p>
</li>
<li><p>保存设置<br><img src="/hexo/.io//7.png" alt="7"><br><img src="/hexo/.io//8.png" alt="8"></p>
</li>
<li><p>设置授权回调地址<br><img src="/hexo/.io//9.png" alt="9"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">注意：这里需要在后端设置路由协同操作，</span><br></pre></td></tr></table></figure>
<p><img src="/hexo/.io//10.png" alt="10"></p>
</li>
</ul>
<h2 id="流程图"><a href="#流程图" class="headerlink" title="流程图"></a>流程图</h2><p><img src="/hexo/.io//11.png" alt="11"></p>
<h2 id="下载SDK插件"><a href="#下载SDK插件" class="headerlink" title="下载SDK插件"></a>下载SDK插件</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install python-alipay-sdk --upgrade</span><br></pre></td></tr></table></figure>
<h2 id="发起支付"><a href="#发起支付" class="headerlink" title="发起支付"></a>发起支付</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">class AliPayBackView(View):</span><br><span class="line"></span><br><span class="line">    def get(self, request):</span><br><span class="line">        user &#x3D; request.user</span><br><span class="line">        # 获取回调的信息</span><br><span class="line">        # 获取 订单号</span><br><span class="line">        sn &#x3D; request.GET.get(&quot;out_trade_no&quot;)</span><br><span class="line">        #  获取 支付宝交易号</span><br><span class="line">        trade_no &#x3D; request.GET.get(&quot;trade_no&quot;)</span><br><span class="line">        print(&quot;sn:&quot;, sn)</span><br><span class="line">        print(&quot;trade_no:&quot;, trade_no)</span><br><span class="line">        # 找到这个订单 并更新状态</span><br><span class="line">        # 修改了  订单状态  并添加了 支付宝交易号</span><br><span class="line">        Order.objects.filter(sn&#x3D;sn).update(trade_no&#x3D;trade_no, status&#x3D;2)</span><br><span class="line">        #  删除我们购物车中的 已经支付成功的购物信息</span><br><span class="line">        print(user)</span><br><span class="line">        # 查询所有的选中 然后删除</span><br><span class="line">        gid_set &#x3D; r.smembers(&quot;select_%d&quot; % user.id)</span><br><span class="line">        print(&quot;gid_set:&quot;, gid_set)</span><br><span class="line">        for gid in gid_set:</span><br><span class="line">            r.hdel(&quot;cart_%d&quot; % user.id, gid)</span><br><span class="line"></span><br><span class="line">        return redirect(&quot;http:&#x2F;&#x2F;127.0.0.1:8080&#x2F;#&#x2F;paysuccess?trade_no&#x3D;&quot; + trade_no)</span><br></pre></td></tr></table></figure>
<h2 id="设置回调"><a href="#设置回调" class="headerlink" title="设置回调"></a>设置回调</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 设置回调地址</span><br><span class="line">class AliPayBackView(View):</span><br><span class="line"></span><br><span class="line">    def get(self, request):</span><br><span class="line">        user &#x3D; request.user</span><br><span class="line">        # 获取回调的信息</span><br><span class="line">        # 获取 订单号</span><br><span class="line">        sn &#x3D; request.GET.get(&quot;out_trade_no&quot;)</span><br><span class="line">        #  获取 支付宝交易号</span><br><span class="line">        trade_no &#x3D; request.GET.get(&quot;trade_no&quot;)</span><br><span class="line">        print(&quot;sn:&quot;, sn)</span><br><span class="line">        print(&quot;trade_no:&quot;, trade_no)</span><br><span class="line">        # 找到这个订单 并更新状态</span><br><span class="line">        # 修改了  订单状态  并添加了 支付宝交易号</span><br><span class="line">        Order.objects.filter(sn&#x3D;sn).update(trade_no&#x3D;trade_no, status&#x3D;2)</span><br><span class="line">        #  删除我们购物车中的 已经支付成功的购物信息</span><br><span class="line">        print(user)</span><br><span class="line">        # 查询所有的选中 然后删除</span><br><span class="line">        gid_set &#x3D; r.smembers(&quot;select_%d&quot; % user.id)</span><br><span class="line">        print(&quot;gid_set:&quot;, gid_set)</span><br><span class="line">        for gid in gid_set:</span><br><span class="line">            r.hdel(&quot;cart_%d&quot; % user.id, gid)</span><br><span class="line"></span><br><span class="line">        return redirect(&quot;http:&#x2F;&#x2F;127.0.0.1:8080&#x2F;#&#x2F;paysuccess?trade_no&#x3D;&quot; + trade_no)</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/01/29/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/" data-id="ckkt9opgb000ki4twdx3c6wjn" data-title="三方支付" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/01/30/celery%E5%BC%82%E6%AD%A5%E6%A1%86%E6%9E%B6(5.0)/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          celery异步框架5.0
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/28/%E5%BE%AE%E5%8D%9A%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">微博三方登录</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>